<template>
    <div class="swiper-container" ref="mySwiper">
        <div class="swiper-wrapper">
            <div
                class="swiper-slide"
                v-for="carousel in carouselList"
                :key="carousel.id"
            >
                <img :src="carousel.imgUrl" />
            </div>
        </div>
        <!-- 如果需要分页器 -->
        <div class="swiper-pagination"></div>

        <!-- 如果需要导航按钮 -->
        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
    </div>
</template>
<script>
import Swiper from 'swiper'
// import 'swiper/css/swiper.min.css'  // 在入口js中引入
export default {
    name: 'Carousel',
    props: {
        carouselList: Array,
    },
    components: {},
    data() {
        return {}
    },
    watch: {
        carouselList: {
            handler() {
                if (this.carouselList.length === 0) return
                //在下次DOM更新, 循环结束之后,执行延迟回调。在 修改数据之后 立即使用这个方法，获取更新后的DOM
                this.$nextTick(() => {
                    new Swiper(this.$refs.mySwiper, {
                        // direction: 'vertical', // 垂直切换选项
                        loop: true, // 循环模式选项

                        // 如果需要分页器
                        pagination: {
                            el: '.swiper-pagination',
                            clickable: true,
                        },

                        // 如果需要前进后退按钮
                        navigation: {
                            nextEl: '.swiper-button-next',
                            prevEl: '.swiper-button-prev',
                        },

                        // 如果需要滚动条
                        // scrollbar: {
                        //     el: '.swiper-scrollbar',
                        // },
                    })
                })
            },
            immediate: true,
        },
    },
    computed: {},
    created() {},
    mounted() {},
    methods: {},
}
</script>
<style lang="less" scoped></style>
